package com.woniuxy.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.entity.Userinfo;
import com.woniuxy.service.UserinfoService;
import com.woniuxy.util.ResponseObj;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@ResponseBody
@RequestMapping("api/v1/users")
public class UserinfoController {

    @Autowired
    private UserinfoService userinfoService;

    @GetMapping
    public ResponseObj queryUserinfo(){
        List<Userinfo> list = userinfoService.list();
        return ResponseObj.ok(list);
    }

    @GetMapping("/{number}/{size}")
    public ResponseObj queryCondition(@PathVariable Integer number,
                                      @PathVariable Integer size, Userinfo userinfo){
        Page<Userinfo> page = new Page<>(number,size);

        QueryWrapper<Userinfo> wrapper = new QueryWrapper<>();
        wrapper.like(userinfo.getUsername()!=null&&userinfo.getUsername().trim().isEmpty(),"username",userinfo.getUsername());
        List<Userinfo> list = userinfoService.list(page, wrapper);
        return ResponseObj.ok(list);
    }

    @GetMapping("/{id}")
    public ResponseObj queryById(@RequestParam Integer id){
        Userinfo byId = userinfoService.getById(id);
        return  ResponseObj.ok(byId);
    }

    @PutMapping
    public ResponseObj addUserinfo(@RequestBody Userinfo userinfo){
        boolean save = userinfoService.save(userinfo);
        return ResponseObj.ok(save);
    }
    @PostMapping("/{id}")
    public ResponseObj updateUserinfo(@RequestParam Integer id,
                                      @RequestBody Userinfo userinfo){
        userinfo.setId(id);
        boolean b = userinfoService.updateById(userinfo);
        return ResponseObj.ok(b);
    }
    @DeleteMapping("/{id}")
    public ResponseObj deleteById(@RequestParam Integer id){
        boolean b = userinfoService.removeById(id);
        return ResponseObj.ok(b);
    }
}
